package com.bosssoft.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bosssoft.entity.dto.FibExpenseBudgetDetailDTO;
import com.bosssoft.entity.dto.FibIncomeBudgetDetailDTO;
import com.bosssoft.entity.vo.*;
import com.bosssoft.page.PageResult;
import com.bosssoft.response.Result;
import com.bosssoft.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import static com.bosssoft.enums.BgtCode.EXPORT_ERROR;
import static com.bosssoft.enums.BgtCode.SAVE_FAIL;

/**
 * @author Li kexi
 * Created on 2021/8/16.21:00
 */
@RestController
@Api(tags = "预算单位用户-预算管理控制器")
@RequestMapping("/unitBudget")
public class UnitFibBudgetController {

    @Autowired
    private FibBudgetService fibBudgetService;
    @Autowired
    private FibBudgetDetailsService fibBudgetDetailsService;
    @Autowired
    private FibExpenseBudgetService fibExpenseBudgetService;
    @Autowired
    private FibExpenseBudgetDetailService fibExpenseBudgetDetailService;
    @Autowired
    private FibIncomeBudgetService fibIncomeBudgetService;
    @Autowired
    private FibIncomeBudgetDetailService fibIncomeBudgetDetailService;


    // 新建单位预算明细（支出和收入分开）同时修改expence_budget、income_budget
     @ApiOperation("新建收入预算明细")
     @PostMapping("/addIncomeDetail")
     public Result addIncomeDetail(@RequestBody @Validated FibIncomeBudgetDetailVO request){
         FibIncomeBudgetDetailDTO detailDTO = new FibIncomeBudgetDetailDTO();
         BeanUtils.copyProperties(request,detailDTO);
         //第一步，添加新的收入预算明细记录
         boolean save = fibIncomeBudgetDetailService.addIncomeDetail(detailDTO);
         if (!save){
             return Result.error(SAVE_FAIL);
         }
         //第二步，根据新的收入明细，修改income_budget

         //某部门某年的收入预算
         Long deptId = request.getDeptId();
         Integer year = request.getCreateTime().getYear();
         String incomeType = request.getIncomeType();
         BigDecimal amount = request.getAmount();
         Long foreign = fibIncomeBudgetService.updateByDetail(deptId,year,incomeType,amount);

         int a = fibIncomeBudgetDetailService.updateForeign(foreign);
         if (a==0){
             return Result.error();
         }
         return Result.ok();
     }

    // 生成 单位年度预算编报（支出和收入分开）
    @PostMapping("/addExpenseDetail")
    @ApiOperation("新建支出预算明细")
    public Result addExpenseDetail(@RequestBody @Validated FibExpenseBudgetDetailVO request){
         FibExpenseBudgetDetailDTO detailDTO = new FibExpenseBudgetDetailDTO();

         BeanUtils.copyProperties(request,detailDTO);
         //第一步，添加新的支出预算明细记录
         boolean save = fibExpenseBudgetDetailService.addExpenseDetail(detailDTO);
         if (!save){
            return Result.error(SAVE_FAIL);
         }
        //第二步，根据新的支出明细，修改expense_budget
        Long ebId = request.getEbId();
        BigDecimal publicBudget = request.getPublicBudget();
        BigDecimal specialFund = request.getSpecialFund();
        BigDecimal amount = publicBudget.add(specialFund);
        Integer year = request.getCreateTime().getYear();
        boolean update = fibExpenseBudgetService.updateByDetail(ebId,amount,year);
        if (!update){
            return Result.error(SAVE_FAIL);
        }
        return Result.ok();
    }

    @PostMapping("/reportForm")
    @ApiOperation("某单位的年度支出预算编报表")
    public Result reportForm(@RequestParam(defaultValue = "1")Integer pageNo,
                             @RequestParam(defaultValue = "10")Integer pageSize,
                             String deptName,
                             Integer year){

        Page<FibExpenseBudgetVO> voPage = fibExpenseBudgetService.selectByPage(pageNo, pageSize, deptName, year);
        PageResult<FibExpenseBudgetVO> voPageResult = new PageResult<>(voPage, voPage.getRecords());
        return Result.ok().data("page",voPageResult);
    }

    @PostMapping("/reportList")
    @ApiOperation("某单位的年度收入预算编报表")
    public Result reportList(String deptName , Integer year){
         Page<FibIncomeBudgetVO> voPage = new Page<>(1, 10);
         FibIncomeBudgetVO budgetVO = fibIncomeBudgetService.selectIncomeBudgetReport(deptName,year);
         List<FibIncomeBudgetVO> budgetVOS = new ArrayList<>();
        budgetVOS.add(budgetVO);
        PageResult<FibIncomeBudgetVO> budget = new PageResult<>(voPage,budgetVOS);
         return Result.ok().data("budget",budget);
    }

//    @ApiOperation("新建预算申请")
//    @PostMapping("/addBudget")
//    public Result addBudget(@Validated FibBudgetVO request){
//
//        FibBudgetDTO fibBudgetDTO = new FibBudgetDTO();
//        BeanUtils.copyProperties(request,fibBudgetDTO);
//        boolean save = fibBudgetService.addBudget(fibBudgetDTO);
//        if (!save){
//            return Result.error();
//        }
//        return Result.ok();
//    }
//
//    @ApiOperation("当前单位的预算申请查询")
//    @PatchMapping("/queryAll")
//    public Result queryAll(@RequestParam(defaultValue = "1")Integer pageNo,
//                                      @RequestParam(defaultValue = "10")Integer pageSize,
//                                      Long bgd_dept){
//
//        Page<FibBudgetVO> page = fibBudgetService.selectMyBgt(pageNo,pageSize,bgd_dept);
//        return page;
//    }
//
//    @ApiOperation("当前单位预算明细查询")
//    @PostMapping("/selectDetials")
//    public Result selectStatemaent(@RequestParam(defaultValue = "1")Integer pageNo,
//                                                     @RequestParam(defaultValue = "10")Integer pageSize,
//                                                     Long bgd_department){
//        Page<FibBudgetDetailsVO> page = fibBudgetDetailsService.selectMyBgtDtl(pageNo,pageSize,bgd_department);
//        return null;
//    }

    @ApiOperation("将表单数据导出为Excel")
    @GetMapping("/exportExcel")
    // Q: 导入excel的数据从哪里来，前端传来什么
    public Result exportExcel(String filePath ,@Validated List<FibBudgetVO> table){

        boolean export = fibBudgetService.export(filePath,table);
        if (!export){
            return Result.error(EXPORT_ERROR);
        }
        return Result.ok();
    }

}
